package org.iteam.core.service.base;

import java.sql.SQLException;
import java.util.List;

import org.iteam.core.Constant;
import org.iteam.core.model.base.BaseParameter;
import org.iteam.core.service.BaseService;
import org.iteam.core.service.BaseServiceInterface;
import org.iteam.core.service.mysql.impl.base.BaseParameterServiceImpl;

public abstract class BaseParameterService extends BaseService<BaseParameter>
		implements BaseServiceInterface<BaseParameter> {
	private static BaseParameterService serviceMysql = null;
	private static BaseParameterService serviceOracle = null;

	public static BaseParameterService getInstance() {
		Constant.DataBaseType dbTypes = Constant.dataBaseType;
		if (dbTypes.equals(Constant.DataBaseType.MYSQL)) {
			return getMySqlInstance();
		} else if (dbTypes.equals(Constant.DataBaseType.ORACLE)) {
			return getOracleInstance();
		}
		return null;
	}

	public static BaseParameterService getMySqlInstance() {
		if (serviceMysql == null)
			serviceMysql = new BaseParameterServiceImpl();
		return serviceMysql;
	}

	public static BaseParameterService getOracleInstance() {
		if (serviceOracle == null)
			serviceOracle = new org.iteam.core.service.oracle.impl.base.BaseParameterServiceImpl();
		return serviceOracle;
	}

	public BaseParameterService() {
		super(BaseParameter.class);
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<BaseParameter> query() throws SQLException {
		String SQL = "SELECT * FROM BASE_PARAMETER ORDER BY NAME ";
		return super.query(SQL);
	}

	@Override
	public long count() throws SQLException {
		String SQL = "SELECT COUNT(1) FROM BASE_PARAMETER";
		return Long.parseLong(super.certain(SQL).toString());
	}
	
	public Object getValueByName(String name) throws SQLException{
		return super.certain("select value from base_parameter where 1=1 and name=?".toUpperCase(),name);
	}

	public long validate(String newName) throws SQLException {
		if (newName == null || "".equals(newName))
			newName = null;
		String SQL = "SELECT COUNT(1) FROM BASE_PARAMETER WHERE NAME = ?";
		return Long.parseLong(super.certain(SQL, newName).toString());
	}

	@Override
	public BaseParameter show(String id) throws SQLException {
		String SQL = "SELECT * FROM BASE_PARAMETER WHERE NAME=?";
		return super.show(SQL, id);
	}

	@Override
	public boolean create(BaseParameter object) throws SQLException {
		return super
				.update("INSERT INTO BASE_PARAMETER(NAME,VALUE,DESCRIPTION) VALUES(?,?,?)",
						object.getName(), object.getValue(),
						object.getDescription());
	}

	@Override
	public boolean update(BaseParameter object) throws SQLException {
		return super.update(
				"UPDATE BASE_PARAMETER SET VALUE=?,DESCRIPTION=? WHERE NAME=?",
				object.getValue(), object.getDescription(), object.getName());
	}

	@Override
	public boolean delete(String id) throws SQLException {
		// TODO Auto-generated method stub
		return destroy(id);
	}

	@Override
	public boolean destroy(String id) throws SQLException {
		return super.update("DELETE FROM BASE_PARAMETER WHERE NAME=?", id);
	}

}
